//
//  ScanVC.swift
//  ImGuider X
//
//  Created by llt on 2018/9/27.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import swiftScan

class ScanVC: LBXScanViewController {
    
    var complete:((_ tripid:String) -> Void)?
    var failture:(()->Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        weixinStyle()
    }
    
    
    private func setupUI() {
        
        self.title = LocalizedString("bind_trip")
        
        setupBackItem(#selector(back))
        
        let rightItem = UIBarButtonItem(title: LocalizedString("photo_album"), style: .plain, target: self, action: #selector(openPhoto))
        
        self.navigationItem.rightBarButtonItem = rightItem
    }
    
    @objc private func back() {
        
        self.failture?()
        
        self.navigationController?.popViewController(animated: true);
    }
    
    @objc private func openPhoto() {
        
        self.openPhotoAlbum()

    }
    
    // MARK: - --无边框，内嵌4个角------
    private func weixinStyle() {
        //设置扫码区域参数
        var style = LBXScanViewStyle()
        style.centerUpOffset = 44
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.Inner
        style.photoframeLineW = 2
        style.photoframeAngleW = 18
        style.photoframeAngleH = 18
        style.isNeedShowRetangle = false
        
        style.anmiationStyle = LBXScanViewAnimationStyle.LineMove
        
        style.colorAngle = UIColor(red: 0.0/255, green: 200.0/255.0, blue: 20.0/255.0, alpha: 1.0)
        
        style.animationImage = UIImage(named: "qrcode_Scan_weixin_Line")
        
        scanStyle = style
    }
    
    override func handleCodeResult(arrayResult: [LBXScanResult]) {
        
        guard let result = arrayResult.first else {
            
            self.view.makeToast(LocalizedString("qrcode_error"))
            self.resetDevice()
            return
        }
        
//        guard let str = result.strScanned,
//              let data = str.data(using: .utf8),
//              let dict = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? Dictionary<String,String>,
//              let gina = dict?["gina"] else {
//
//                self.view.makeToast(LocalizedString("qrcode_tips"))
//                self.resetDevice()
//                return
//        }
        
       //二维码格式修改
        guard let str = result.strScanned , str.hasPrefix(kDomain),let gina = str.components(separatedBy: "viewid=").last else {
                
                self.view.makeToast(LocalizedString("qrcode_tips"))
                self.resetDevice()
                return
        }
        print("======" + gina)
        
        complete?(gina)
        
        if UserManager.share.token?.count ?? 0 == 0 {
            
            let sb = UIStoryboard(name: "Login", bundle: nil)
            let loginVC = sb.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
            
            loginVC.pageType = 1
            loginVC.bindtripid = gina
            
            self.navigationController?.pushViewController(loginVC, animated: true)
            
            return
        }
    
        self.navigationController?.popViewController(animated: true)
    }
    
    private func resetDevice() {
        
        DispatchQueue.main.after(1) {
            
            [weak self] in
            guard let `self` = self else { return }
            
            self.startScan()
        }
    }
}

